package com.provider.test;

import com.common.test.coderesolver.RpcDecoder;
import com.common.test.coderesolver.RpcEncoder;
import com.common.test.entity.RpcRequest;
import com.common.test.serializer.JSONSerializer;
import com.provider.test.config.NettyConfig;
import com.provider.test.netty.ServerBootStrap;
import com.provider.test.netty.UserServerHandler;
import com.provider.test.registry.RegistryBO;
import com.provider.test.registry.RegistryCenterService;
import com.provider.test.registry.RegistryIPPortBO;
import com.provider.test.registry.impl.RegistryCenterServiceImpl;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.StringUtils;

import java.util.Properties;


@SpringBootApplication
public class ProviderApplication  {



    private static final Logger logger = LoggerFactory.getLogger(ProviderApplication.class);


    public static void main(String[] args) {

        SpringApplication.run(ProviderApplication.class, args);
        try {
            registry();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

  private static void registry() throws InterruptedException {
      Properties properties = System.getProperties();
       String portTest = properties.getProperty("portTest");
      Integer port=NettyConfig.WS_PORT;
      if (!StringUtils.isEmpty(portTest)){
          port=Integer.valueOf(portTest);
      }
      RegistryIPPortBO registryIPPortBO =new RegistryIPPortBO(NettyConfig.WS_HOST,port.toString());
      RegistryBO registryBO=new RegistryBO();
      registryBO.setRegistryIPPortBO(registryIPPortBO);
      registryBO.setServerName("userService");
      RegistryCenterService registryCenterService =new RegistryCenterServiceImpl();
      registryCenterService.registryServer(registryBO);
      startServer(NettyConfig.WS_HOST,port);
  }

    public static void startServer(String hostName,int port) throws InterruptedException {

        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup,workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new RpcEncoder(RpcRequest.class, new JSONSerializer()));
                        pipeline.addLast( new RpcDecoder(RpcRequest.class, new JSONSerializer()));
                        /*pipeline.addLast(new StringDecoder());
                        pipeline.addLast( new StringEncoder() );*/
                        pipeline.addLast(new UserServerHandler());

                    }
                });
        serverBootstrap.bind(hostName,port).sync();
    }
}
